import React, { useEffect, useState } from 'react';
import { Form, Button, Space, notification } from 'antd';
import { componentsMap, goBack, checkStatusMap } from '@/utils/util';
import FileList from '@/components/FileLists';
import UploadList from '@/components/UploadList';
import API from '@/api/xjindex.js';
import moment from 'moment';
import { useModel } from 'umi';

function Handle({ history }) {
  const [form] = Form.useForm();
  const [fileList, setfileList] = useState([]);
  const { initialState } = useModel('@@initialState');
  const { username, userId, orgName, orgId } = initialState.info;
  form.setFieldsValue({
    orgName: orgName,
    username: username,
    dtime: moment().format('YYYY-MM-DD'),
  });
  const TableList = [
    {
      name: 'orgName',
      label: '所属单位',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      name: 'status',
      label: '报告状态',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      name: 'createName',
      type: 'input',
      label: '创建人',
      otherProps: {
        disabled: true,
      },
    },
    {
      name: 'createTime',
      type: 'input',
      label: '创建时间',
      otherProps: {
        disabled: true,
      },
    },
    {
      name: 'remarks',
      type: 'textarea',
      label: '报告说明',
      otherProps: {
        disabled: true,
      },
      isremark: true,
    },
  ];
  const formList = [
    {
      name: 'orgName',
      title: '处置单位',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      name: 'dtime',
      title: '处置时间',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      name: 'username',
      type: 'input',
      title: '处置人',
      otherProps: {
        disabled: true,
      },
    },
    {
      name: 'dremakes',
      type: 'textarea',
      title: '处置说明',
      isBlock: true,
    },
  ];
  const uploadOptions = {
    fileTypes: [
      'application/doc',
      'application/pdf',
      'application/msword',
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    ],
    maxSize: 50,
    maxNum: 3,
    accept: [
      '.doc,.docx.,.pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    ],
    acceptMessage: 'word/pdf',
  };

  function initList() {
    API.findSecurityCheck({ id: history.location.query.id }).then((res) => {
      if (res) {
        form.setFieldsValue({ ...res.t, status: checkStatusMap(res.t.status) });
        setfileList(res.t.securityCheckFileList);
      }
    });
  }
  const submitFn = (values) => {
    const params = {
      dorgId: orgId,
      dremarks: values.dremakes,
      securityId: history.location.query.id,
      username: username,
      userId: userId,
      disposalResultFileList: values.file,
    };
    API.addDr(params).then((res) => {
      if (res) {
        notification.success({
          message: '修改成功',
        });
        goBack();
      }
    });
  };
  useEffect(() => {
    initList();
  }, []);
  return (
    <div className="main-x">
      <Form form={form} onFinish={submitFn}>
        <div className="base-title mt20">检查报告信息</div>
        <div className="ant-form-inline form-x bgf">
          {TableList.map((item) => {
            return (
              <Form.Item
                key={item.name}
                label={item.label}
                name={item.name}
                disabled={item.disabled}
                className={item.isremark ? 'block' : ''}
              >
                {componentsMap(item.type, item.otherProps)}
              </Form.Item>
            );
          })}
          <Form.Item label="附件信息" className="block">
            {
              <FileList
                fileList={fileList}
                downloadUrl="/securityCheck/downloadSc"
              />
            }
          </Form.Item>
        </div>
        <div className="base-title mt20">处置结果</div>
        <div className="ant-form-inline form-x bgf">
          {formList.map((item) => {
            return (
              <Form.Item
                key={item.name}
                label={item.title}
                name={item.name}
                className={item.isBlock ? 'block' : ''}
              >
                {componentsMap(item.type, item.otherProps)}
              </Form.Item>
            );
          })}
          <Form.Item
            label="附件信息"
            extra="支持jpg/word/pdf格式,单个大小在50M以内,支持多个上传，最多上传3个附件"
            name="file"
            className="block"
          >
            <UploadList
              uploadUrl="/securityCheck/fileUpload "
              downloadUrl="/securityCheck/downloadDr"
              options={uploadOptions}
            ></UploadList>
          </Form.Item>
        </div>
        <div className="footer-x">
          <Space size={18}>
            <Button type="primary" htmlType="submit">
              提交
            </Button>
            <Button className="cancel-btn" type="primary" onClick={goBack}>
              返回
            </Button>
          </Space>
        </div>
      </Form>
    </div>
  );
}

export default Handle;
